/**
 * 圈子动态Adapter
 */
package cn.suse.android.adapter;

import java.util.List;

import cn.suse.android.R;
import cn.suse.android.entity.Circle;
import cn.suse.android.share.MainApplication;
import cn.suse.android.utils.StringUtils;
import cn.suse.android.utils.TimeUtils;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

/**
 * @author liubin
 * Create DateTime:Oct 16, 2012 9:30:47 AM
 * Description:
 * @Version: 1.0.0
 */
public class CircleListViewAdapter extends BaseAdapter {

	private List<Circle> lists;
	private Context context;
	
	
	public CircleListViewAdapter(List<Circle> circleList, Context context) {
		this.lists = circleList;
		this.context = context;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getCount()
	 */
	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return lists == null?0:lists.size();
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItem(int)
	 */
	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return lists == null?null:lists.get(position);
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getItemId(int)
	 */
	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	/* (non-Javadoc)
	 * @see android.widget.Adapter#getView(int, android.view.View, android.view.ViewGroup)
	 */
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		ViewHolder holder = null;
		if(convertView == null){
			convertView = LayoutInflater.from(context).inflate(R.layout.repaste_listitem, null);
			holder = new ViewHolder();
			holder.contentTextView =(TextView) convertView.findViewById(R.id.content);
			holder.headerImageView =(ImageView)convertView.findViewById(R.id.header);
			holder.timeTextView = (TextView)convertView.findViewById(R.id.time);
//			holder.titleTextView = (TextView)convertView.findViewById(R.id.title);
			holder.usernameTextView = (TextView)convertView.findViewById(R.id.username);
			holder.sourceImageView = (ImageView)convertView.findViewById(R.id.image);
			convertView.setTag(holder);
		}else{
			holder = (ViewHolder) convertView.getTag();
		}
		Circle circle = lists.get(position);
		holder.contentTextView.setText(circle.getTxt().replaceAll("&nbsp;", "").trim());
		if(!StringUtils.isEmpty(circle.getUser().getLogo50())||circle.getUser().getLogo50().endsWith(".jpg")){
			MainApplication.bitmapManager.loadBitmap(circle.getUser().getLogo50(), holder.headerImageView);
		}else{
			holder.headerImageView.setImageResource(R.drawable.widget_dface_loading);
		}
		if(StringUtils.isImage(circle.getS_pic())){
			holder.sourceImageView.setVisibility(View.VISIBLE);
			MainApplication.bitmapManager.loadBitmap(circle.getS_pic(), holder.sourceImageView);
		}else if(StringUtils.isImage(circle.getImg_url())){
			holder.sourceImageView.setVisibility(View.VISIBLE);
			MainApplication.bitmapManager.loadBitmap(circle.getImg_url(), holder.sourceImageView);
		}
		holder.timeTextView.setText(TimeUtils.friendly_time(circle.getcTime()));
//		holder.titleTextView.setText(circle.getTitle());
		holder.usernameTextView.setText(circle.getUser().getName());
		return convertView;
	}

	public List<Circle> getCircleList() {
		return lists;
	}

	public void setCircleListView(List<Circle> circleList) {
		this.lists = circleList;
		this.notifyDataSetChanged();
	}
	private static class ViewHolder{
		ImageView headerImageView;
		TextView usernameTextView;
		TextView timeTextView;
//		TextView titleTextView;
		TextView contentTextView;
		ImageView sourceImageView;
	}
}

